package livesun.justiceeternal.db.task;

import org.greenrobot.greendao.AbstractDao;

import livesun.justiceeternal.BuildConfig;
import livesun.justiceeternal.db.DaoSession;


public abstract class AbsTemplateDbTask<T> extends AbsDbTask {
    private Class<? extends T> mClass;

    public AbsTemplateDbTask(Class<? extends T> t) {
        mClass = t;
    }

    @Override
    public final void onTaskExecute(DaoSession t) {
        AbstractDao<T, ?> dao = initDao(t);
        onTaskExecute(dao);
    }

    public abstract void onTaskExecute(AbstractDao<T, ?> dao);


    protected final AbstractDao<T, ?> initDao(DaoSession t) {
        AbstractDao<T, ?> dao;
        try {
            dao = (AbstractDao<T, ?>) t.getDao(mClass);
        } catch (Throwable e) {
            if (BuildConfig.DEBUG) {
                throw new RuntimeException(e);
            } else {
                dao = null;
            }
        }
        return dao;
    }
}
